package com.baiytfp.hf.HFDataExchange.action.dao;

import com.baiytfp.hf.HFDataExchange.common.model.state.TreeType;
import com.baiytfp.hf.HFDataExchange.common.model.table.Dept;
import com.baiytfp.hf.HFDataExchange.common.model.table.Tree;
import com.baiytfp.hf.HFDataExchange.common.model.table.User_dept;
import com.baiytfp.hf.baiytfp.common.db.DBManager;
import com.baiytfp.hf.baiytfp.framework.base.BaseDao;
import com.baiytfp.hf.baiytfp.framework.model.base.TableName;
import com.baiytfp.hf.baiytfp.framework.model.state.YesOrNo;
import com.baiytfp.hf.baiytfp.hfutils.HFDBHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.springframework.stereotype.Repository;

@Repository
public class DeptDAOImpl extends BaseDao {
	
	/**
	 * 判断部门是否为初始化数据
	 * @param treeCode
	 * @return
	 * @throws Exception
	 */
	public boolean isInit(String treeCode) throws Exception {
		
		boolean flag = false;
		
		int treeType = TreeType.USER_DEPT.getKey();
		
		Tree treeVO = HFDBHelper.read(TableName.TREE, " where tree_code = ? ",Tree.class, new Object[]{treeCode});
		if(treeVO!=null){
			Dept deptVO = HFDBHelper.read(TableName.DEPT, " where tree_id =? and is_init = ? ",Dept.class, new Object[]{treeVO.getId(),YesOrNo.YES.getKey()});
			if(deptVO!=null){
				flag = true;
			}
		}
		return flag;
	}
	
	/**
	 * 判断是否存在员工
	 * @param treeVO
	 * @return
	 * @throws Exception
	 */
	public boolean isHasStaff(Tree treeVO) throws Exception {
		StringBuffer sql=new StringBuffer(" select * from "+TableName.USER_DEPT+" ud ");
		sql.append(" left join "+TableName.USER_ACCOUNT+" ua on ud.user_code=ua.user_code ");
		
		String sWhere=" where ud.dept_id=? ";
		
		long res=HFDBHelper.rownum(null,sql.toString()+sWhere, new String[]{treeVO.getId()});
		if(res > 0 ){
			return true;
		}
		return false;
	}
	
	/**
	 * 删除部门
	 * @param treeVO
	 * @throws Exception
	 */
	public void tnodedel(Tree treeVO) throws Exception{
		
		Connection connection = null;
		try {
			connection = DBManager.getConnection(); // 获取连接池
			DBManager.beginTransaction(connection); // 打开事务

			HFDBHelper.delete(connection, TableName.DEPT, " where tree_id = ? ", new Object[]{treeVO.getId()});

			HFDBHelper.delete(connection, TableName.TREE, " where id =?  ", new Object[]{treeVO.getId()});

			if(!"-1".equals(treeVO.getParent_tree_code())){
				Tree parentTree = HFDBHelper.read(connection, TableName.TREE, " where TREE_TYPE = ? and TREE_CODE=?  ", Tree.class, new Object[]{ treeVO.getTree_type() + "", treeVO.getParent_tree_code()});

				parentTree.setChildren_number(parentTree.getChildren_number() - 1);

				HFDBHelper.update(connection, TableName.TREE, " where id=? ", parentTree, parentTree.getId() );
			}

			DBManager.commitTransaction(connection); // 提交事务
		} catch (SQLException se) {
			DBManager.rollbackTransaction(connection); // 回滚事务
			throw se;
		} finally {
			DBManager.close(connection); // 涉及事务且外面传进来的连接不在这里关闭
		}
	}

	/**
	 * 获取该部门的所有用户
	 * @param dept_id
	 * @return
	 * @throws Exception
	 */
	public List<User_dept> getHasUserCodeByUserDept(String dept_id) throws Exception {
		return HFDBHelper.list(TableName.USER_DEPT, " where dept_id=? ", User_dept.class, new Object[]{dept_id});
	}
}
